﻿Imports SdlDotNet.Graphics
Imports SdlDotNet.Graphics.Sprites
Imports System.Drawing
Imports System.Timers

Public Class NuclearExplosion
    Implements ITemporaryDrawableEntity

    Private ExplosionSprite As AnimatedSprite
    Private WithEvents AnimationTimer As New Timer(50)

    Public Event NoLongerInUse(ByRef sender As ITemporaryDrawableEntity) Implements ITemporaryDrawableEntity.NoLongerInUse

    Public Sub New(ByVal position As Point)
        CommonSounds.Explosion.Play()

        ExplosionSprite = New AnimatedSprite(CommonSurfaces.Instance().NuclearExplosionSurfaces, New Point(position.X - 15, position.Y - 20))
        ExplosionSprite.Animate = False
        ExplosionSprite.Animations.Loop = False

        AnimationTimer.Start()
    End Sub

    Private Sub AnimationTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles AnimationTimer.Elapsed
        If (ExplosionSprite.Frame = ExplosionSprite.Animations("Default").Count - 1) Then
            RaiseEvent NoLongerInUse(Me)
            AnimationTimer.Stop()
            ExplosionSprite.Visible = False
        Else
            ExplosionSprite.Frame += 1
        End If
    End Sub

    Public Sub Dispose() Implements ITemporaryDrawableEntity.Dispose
        ExplosionSprite.Surface = Nothing
        ExplosionSprite.Dispose()
        AnimationTimer.Dispose()
    End Sub

    Public Sub Draw(ByRef display As SdlDotNet.Graphics.Surface) Implements ITemporaryDrawableEntity.Draw
        display.Blit(ExplosionSprite)
    End Sub

End Class